package org.wyf.EveryPoint.crowd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.crowd.entity.Project;
import org.wyf.EveryPoint.crowd.entity.Reward;
import org.wyf.EveryPoint.crowd.service.IRewardService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.wyf.EveryPoint.common.utils.FebsUtil;
/**
 * 回报设置 Controller
 *
 * @author wyf
 * @date 2021-04-09 16:56:39
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class RewardController extends BaseController {

    private final IRewardService rewardService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "reward")
    public String rewardIndex(){
        return FebsUtil.view("reward/reward");
    }

    @GetMapping("reward")
    @ResponseBody
    @RequiresPermissions("reward:list")
    public FebsResponse getAllRewards(Reward reward) {
        return new FebsResponse().success().data(rewardService.findRewards(reward));
    }

    @GetMapping("reward/list")
    @ResponseBody
    @RequiresPermissions("reward:list")
    public FebsResponse rewardList(QueryRequest request, Reward reward) {
        Map<String, Object> dataTable = getDataTable(this.rewardService.findRewards(request, reward));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Reward", exceptionMessage = "新增Reward失败")
    @PostMapping("reward")
    @ResponseBody
    @RequiresPermissions("reward:add")
    public FebsResponse addReward(@Valid Reward reward) {
        this.rewardService.createReward(reward);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Reward", exceptionMessage = "删除Reward失败")
    @GetMapping("reward/delete")
    @ResponseBody
    @RequiresPermissions("reward:delete")
    public FebsResponse deleteReward(Reward reward) {
        this.rewardService.deleteReward(reward);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Reward", exceptionMessage = "修改Reward失败")
    @PostMapping("reward/update")
    @ResponseBody
    @RequiresPermissions("reward:update")
    public FebsResponse updateReward(Reward reward) {
        this.rewardService.updateReward(reward);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Reward", exceptionMessage = "导出Excel失败")
    @PostMapping("reward/excel")
    @ResponseBody
    @RequiresPermissions("reward:export")
    public void export(QueryRequest queryRequest, Reward reward, HttpServletResponse response) {
        List<Reward> rewards = this.rewardService.findRewards(queryRequest, reward).getRecords();
        ExcelKit.$Export(Reward.class, response).downXlsx(rewards, false);
    }


    /**
     * 创建回报
     * @param data
     * @return
     */
    @ControllerEndpoint(operation = "创建回报", exceptionMessage = "创建回报失败")
    @PostMapping("/web/updateProjectReward")
    @ResponseBody
    public FebsResponse updateProjectReward(@RequestBody Map<String,Map<String,String>> data) {
        // 有偿
        for (int i = 0; i < data.size();i++){
            Reward reward= new Reward();

            if (data.get(i+"").get("id") != null && !data.get(i+"").get("id").equals("")){
                reward.setId(data.get(i+"").get("id"));
            }
            reward.setPid(Long.parseLong(data.get(i+"").get("pid")));
            reward.setSupport(Long.parseLong(data.get(i+"").get("support")));
            reward.setTitle(data.get(i+"").get("title"));
            reward.setIntroduce(data.get(i+"").get("introduce"));
            reward.setProvideYear(Integer.parseInt(data.get(i+"").get("provideYear")));
            reward.setProvideMonth(Integer.parseInt(data.get(i+"").get("provideMonth")));
            reward.setType("1");
            rewardService.saveOrUpdate(reward);
        }
//        无偿
        Reward reward= new Reward();
        reward.setPid(Long.parseLong(data.get(0+"").get("pid")));
        reward.setSupport(0L);
        reward.setTitle("支持梦想，不需要回报");
        reward.setIntroduce("不需要回报，我只是支持有梦想的人。");
        reward.setProvideYear(9999);
        reward.setProvideMonth(13);
        reward.setType("0");
        Long pid = Long.parseLong(data.get(0+"").get("pid"));
        rewardService.saveOrUpdate(reward);
        return new FebsResponse().success().data(pid);
    }

    /**
     * 获取回报
     * @param reward
     * @return
     */
    @ControllerEndpoint(operation = "获取回报", exceptionMessage = "获取回报失败")
    @PostMapping("/web/reward/getProjectReward")
    @ResponseBody
    public FebsResponse getProjectReward(Reward reward) {
        QueryWrapper<Reward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",reward.getPid()).eq("type","1");
        List<Reward> data = rewardService.list(queryWrapper);
        return new FebsResponse().success().data(data);
    }
}
